const app = getApp();
const ajax = function (options) {
	const method = options.method || 'POST';
	const data = options.data || {};
	let header = {};
	let url = app.globalData.url + options.url;

	wx.request({
		method: method,
		url: url,
		data: data,
		header: header,
		success(res) {
			const statusCode = res.statusCode;
			if (statusCode != 200) {
				if (res && res.data && res.data.message) {
					wx.showToast({
						title: res.data.message,
						icon: 'none',
					});
				}
				if (typeof (options.fail) == 'function') {
					options.fail(res);
				}
				return;
			}
			const data = res.data;
			if (data.code != 200) {
				if (typeof (options.fail) == 'function') {
					options.fail(res);
				}
				return;
			}

			options.success && options.success(data.data);
		},
		fail(res) {
			if (typeof (options.fail) == 'function') {
				options.fail(res);
			}
		}
	});
};
const alert = function (msg) {
	return new Promise((resolve, reject) => {
		if (!msg) return reject('message为空');
		wx.showToast({
			title: msg,
			icon: 'none',
			duration: 1500
		})
		setTimeout(() => {
			resolve()
		}, 1300)
	})
}

module.exports = {
	ajax,
	alert
}